package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;

import com.zhusx.core.helper.Lib_SelectPhotoHelper;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Arrays;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.ModifyBossData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;
import lumingweihua.future.cn.lumingweihua.widget.dialog.BottomListDialog;

/**
 * todo 货主认证 第一步
 * Created by z1986 on 2018/3/17.
 */

public class ModifyGoodsBossActivity extends BaseCompatActivity {
    final int RESULT_ID = 13;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_id)
    EditText editId;
    @BindView(R.id.iv_image)
    FrescoImageView ivImage;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    @BindView(R.id.layout_content)
    View contentLayout;

    Lib_SelectPhotoHelper helper;
    BottomListDialog dialog;
    ModifyBossData en;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_boss_author);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        helper = new Lib_SelectPhotoHelper(this);
        dialog = new BottomListDialog(this).setData(_Arrays.asList("相册", "拍照"), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        helper._startSelectPhoto(ModifyGoodsBossActivity.this);
                        break;
                    case 1:
                        helper._startTakePhoto(ModifyGoodsBossActivity.this);
                        break;
                }
                dialog.dismiss();
            }
        });
        LoadData<ModifyBossData> loadData = new LoadData<>(LoadData.Api.货主认证信息显示, this);
        loadData._setOnLoadingListener(new LoadingHelper<ModifyBossData>(contentLayout, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<ModifyBossData> result) {
                initData(result.getData());
            }
        });
        loadData._loadData();
    }

    private void initData(ModifyBossData data) {
        this.en = data;
        editName.setText(data.linkname);
        editId.setText(data.id_num);
        ivImage.setImageURI(data.hand_img);
    }

    @OnClick({R.id.tv_submit, R.id.iv_image, R.id.iv_delete})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit:
                String name = editName.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    showToast("请输入真实姓名");
                    return;
                }
                String userId = editId.getText().toString();
                if (userId.length() != 15 && userId.length() != 18) {
                    showToast("请输入正确的身份证号码");
                    return;
                }
                if (TextUtils.isEmpty(en.hand_img) && en.hand_imgFile == null) {
                    showToast("请选择身份证正面照");
                    return;
                }
                en.linkname = name;
                en.id_num = userId;
                startActivityForResult(new Intent(v.getContext(), ModifyGoodsBoss2Activity.class)
                        .putExtra(_EXTRA_Serializable, en), RESULT_ID);
                break;
            case R.id.iv_image:
                if (en.hand_imgFile != null) {
                    startActivity(new Intent(v.getContext(), PhotoActivity.class).putExtra(_EXTRA_String, Uri.fromFile(new File(en.hand_imgFile)).toString()));
                } else if (ivImage.getVisibility() == View.VISIBLE && !TextUtils.isEmpty(en.hand_img)) {
                    startActivity(new Intent(v.getContext(), PhotoActivity.class).putExtra(_EXTRA_String, en.hand_img));
                } else {
                    dialog.show();
                }
                break;
            case R.id.iv_delete:
                en.hand_imgFile = null;
                en.hand_img = null;
                ivDelete.setVisibility(View.GONE);
                ivImage.setImageURI(Uri.parse("res://" + getPackageName() + "/" + R.drawable.iv_tianji));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_ID && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        } else {
            helper._onActivityResult(requestCode, resultCode, data, new Lib_SelectPhotoHelper.onDataListener() {
                @Override
                public void onPhoto(File file) {
                    en.hand_imgFile = compressFile(file).getPath();
                    ivImage.setImageURI(Uri.fromFile(new File(en.hand_imgFile)));
                    ivDelete.setVisibility(View.VISIBLE);
                }
            });
        }
    }
}
